home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlcomp / adlobj.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  299 lines

  1.     /***************************************************************\
  2.     *                                *
  3.     *    adlobj.c - routines dealing with the compilation of    *
  4.     *    object declaractions and assigments.            *
  5.     *    Copyright 1987 by Ross Cunniff.                *
  6.     *                                *
  7.     \***************************************************************/
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "adltypes.h"
  12. #include "adlprog.h"
  13. #include "adldef.h"
  14. #include "adlcomp.h"
  15.  
  16.  
  17.     /***************************************************************\
  18.     *                                *
  19.     *    noun_exists( adj, noun ) - returns an object ID iff    *
  20.     *    an object exists with noun ID noun and adjective id    *
  21.     *    adj.  Returns -1 otherwise.                *
  22.     *                                *
  23.     \***************************************************************/
  24.  
  25. int16
  26. noun_exists( adj, noun )
  27. int16
  28.     adj,
  29.     noun;
  30. {
  31.     int16
  32.     t;
  33.  
  34.     for( t = nounspace[ noun ]; t != 0; t = objspace[ t ].others )
  35.     if( objspace[ t ].adj == adj )
  36.         return t;
  37.     return -1;
  38. }
  39.  
  40.  
  41.     /***************************************************************\
  42.     *                                *
  43.     *    move_obj( obj, dest ) - moves object obj to object    *
  44.     *    dest, taking care to put obj at the END of the list    *
  45.     *    of objects contained in dest.                *
  46.     *                                *
  47.     \***************************************************************/
  48.  
  49. move_obj( obj, dest )
  50. int16
  51.     obj, dest;
  52. {
  53.     int16
  54.     t;
  55.  
  56.     /* Find the object whose link is obj (if any) */
  57.     t = objspace[ obj ].loc;
  58.     if( objspace[ t ].cont != obj ) {
  59.     /* Obj must be a link */
  60.     t = objspace[ t ].cont;
  61.     while( objspace[ t ].link != obj )
  62.         t = objspace[ t ].link;
  63.     objspace[ t ].link = objspace[ objspace[ t ].link ].link;
  64.     }
  65.     else
  66.     /* Obj is the head of the list. */
  67.     objspace[ t ].cont = objspace[ objspace[ t ].cont ].link;
  68.  
  69.     /* Seek to the end of the list and place obj there. */
  70.     objspace[ obj ].loc = dest;
  71.     t = objspace[ dest ].cont;
  72.     objspace[ obj ].link = 0;
  73.     if( t ) {
  74.     /* Jump to the end of the list */
  75.     while( objspace[ t ].link )
  76.         t = objspace[ t ].link;
  77.     objspace[ t ].link = obj;
  78.     }
  79.     else
  80.     /* There is nothing in the list. */
  81.     objspace[ dest ].cont = obj;
  82. }
  83.  
  84.  
  85.     /***************************************************************\
  86.     *                                *
  87.     *    new_noun( mod, noun ) - create a new object with    *
  88.     *    modifier list mod and noun ID noun.  The object is    *
  89.     *    initially placed in .ALL.                *
  90.     *                                *
  91.     \***************************************************************/
  92.  
  93. int16
  94. new_noun( mod, noun )
  95. int16
  96.     mod, noun;
  97. {
  98.     int16
  99.     obj;
  100.  
  101.     obj = NUMOBJ++;
  102.     objspace[ obj ].others = nounspace[ noun ];
  103.     nounspace[ noun ] = obj;
  104.     objspace[ obj ].loc = _ALL;
  105.     objspace[ obj ].link = objspace[ _ALL ].cont;
  106.     objspace[ _ALL ].cont = obj;
  107.     objspace[ obj ].adj = mod;
  108.     objspace[ obj ].noun = noun;
  109.     return obj;
  110.  
  111.  
  112.     /***************************************************************\
  113.     *                                *
  114.     *    getnew( t_read ) - read a new modified object from the    *
  115.     *    token stream and return its id.    t_read is nonzero    *
  116.     *    iff the first token in the list has already been read.    *
  117.     *                                *
  118.     \***************************************************************/
  119.  
  120. int16
  121. getnew( t_read )
  122. int16
  123.     t_read;
  124. {
  125.     int16
  126.     t_save;
  127.  
  128.     if( !t_read )
  129.     lexer();
  130.     if( t_type == VERB ) {
  131.     t_val = -t_val;
  132.     t_type = ADJEC;
  133.     }
  134.     if( t_type == ADJEC ) {
  135.     t_save = t_val;
  136.     lexer();
  137.     if( t_type == UNDECLARED ) {
  138.         insertkey( token, NOUN, NUMNOUN, 1 );
  139.         return new_noun( t_save, NUMNOUN++ );
  140.     }
  141.     else if( t_type == NOUN ) {
  142.         if(        (noun_exists( t_save, t_val ) >= 0) ||
  143.             (noun_exists( 0, t_val ) >= 0) )
  144.         error( "Attempt to redeclare a noun.\n" );
  145.         else
  146.         return new_noun( t_save, t_val );
  147.     }
  148.     else
  149.         error( NOUN_WANTED );
  150.     }
  151.     else if( t_type == UNDECLARED ) {
  152.     insertkey( token, NOUN, NUMNOUN, 1 );
  153.     return new_noun( 0, NUMNOUN++ );
  154.     }
  155.     else
  156.     error( ILLEGAL_SYMBOL );
  157.     return -1;
  158. }
  159.  
  160.  
  161.     /***************************************************************\
  162.     *                                *
  163.     *    getold( t_read, t_save ) - read a previously declared    *
  164.     *    object from the token string.  t_read is the number    *
  165.     *    of tokens already read, and t_save is the value of the    *
  166.     *    previous token (if such exists).            *
  167.     *                                *
  168.     \***************************************************************/
  169.  
  170. int16
  171. getold( t_read, t_save )
  172. int16
  173.     t_read,
  174.     t_save;
  175. {
  176.     if( t_read == 0 )
  177.     lexer();
  178.     if( t_read != 2 ) {
  179.     if( t_type == NOUN_SYN )
  180.         return t_val;
  181.     if( t_type == VERB ) {
  182.         t_type = ADJEC;
  183.         t_val = -t_val;
  184.     }
  185.     if( t_type == ADJEC ) {
  186.         t_save = t_val;
  187.         lexer();
  188.     }
  189.     }
  190.     if( t_type != NOUN ) {
  191.     error( ILLEGAL_SYMBOL );
  192.     return -1;
  193.     }
  194.     if( (t_save = noun_exists( t_save, t_val )) < 0 )
  195.     error( ATTEMPT );
  196.     return t_save;
  197. }
  198.  
  199.  
  200.     /***************************************************************\
  201.     *                                *
  202.     *    setprop( obj, which, val ) - set the which'th property    *
  203.     *    of object obj to be val.                *
  204.     *                                *
  205.     \***************************************************************/
  206.  
  207. setprop( obj, which, val )
  208. int16
  209.     obj,
  210.     which,
  211.     val;
  212. {
  213.     static char
  214.     *ALREADY = "Noun property already assigned.\n";
  215.  
  216.     if( (which >=1) && (which <= 16) ) {
  217.     /* Boolean property */
  218.     if( objspace[ obj ].props1to16 & bitpat[ which - 1 ] )
  219.         warning( ALREADY );
  220.     if( val )
  221.         objspace[ obj ].props1to16 |= bitpat[ which - 1 ];
  222.     else
  223.         objspace[ obj ].props1to16 &= ibitpat[ which - 1 ];
  224.     }
  225.     else if( (which >= 17) && (which <= _ACT ) ) {
  226.     if( objspace[ obj ].props[ which - 17 ] )
  227.         warning( ALREADY );
  228.     objspace[ obj ].props[ which - 17 ] = val;
  229.     }
  230.     else
  231.     error( "Invalid object property number.\n" );
  232. }
  233.   
  234.  
  235. /***************************************************************\
  236. *                                *
  237. *    nounassign( t_read, t_save ) - parse and interpret a    *
  238. *    noun property assignment.                *
  239. *                                *
  240. \***************************************************************/
  241.  
  242. nounassign( t_read, t_save )
  243. int16
  244.     t_read,
  245.     t_save;
  246. {
  247.     int16
  248.     obj,
  249.     which,
  250.     getassign();
  251.  
  252.     obj = getold( t_read, t_save );
  253.     lexer();
  254.     if( t_type != '(' )
  255.     _ERR_FIX( LEFT_EXPECTED, ';' );
  256.     lexer();
  257.     if( t_type != CONST )
  258.     _ERR_FIX( CONST_EXPECTED, ';' );
  259.     which = t_val;
  260.     lexer();
  261.     if( t_type != ')' )
  262.     _ERR_FIX( RIGHT_EXPECTED, ';' );
  263.     setprop( obj, which, getassign( 0 ) );
  264. }
  265.  
  266.  
  267.     /***************************************************************\
  268.     *                                *
  269.     *    getnouns() - parse and interpret a NOUN declaration.    *
  270.     *                                *
  271.     \***************************************************************/
  272.  
  273. getnouns()
  274. {
  275.     int16
  276.     obj, loc;
  277.  
  278.     while( t_type != ';' ) {
  279.     if( (obj = getnew( 0 )) >= 0 ) {
  280.         lexer();
  281.         if( t_type == '(' ) {
  282.         loc = getold( 0, 0 );
  283.         move_obj( obj, loc );
  284.         lexer();
  285.         if( t_type != ')' )
  286.             _ERR_FIX( RIGHT_EXPECTED, ';' );
  287.         lexer();
  288.         }
  289.         else if( (t_type != ',') && (t_type != ';') )
  290.         _ERR_FIX( COMMA_EXPECTED, ';' );
  291.     }
  292.     else
  293.         eatuntil( ';' );
  294.     }
  295. }
  296.  
  297. /*** EOF adlobj.c ***/
  298.